<template>
  <div class="customer-layout">
    <nav class="navbar">
      <div class="nav-brand">
        <h2>在线商城</h2>
      </div>
      <div class="nav-menu">
        <router-link to="/customer/products" class="nav-link">
          商品列表
        </router-link>
        <router-link to="/customer/cart" class="nav-link cart-link">
          购物车
          <span v-if="cartItemCount > 0" class="cart-badge">{{ cartItemCount }}</span>
        </router-link>
        <router-link to="/customer/orders" class="nav-link">
          我的订单
        </router-link>
        <div class="user-info">
          <span>{{ user?.name }}</span>
          <button @click="logout" class="logout-btn">退出</button>
        </div>
      </div>
    </nav>
    
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../stores/auth'
import { useCartStore } from '../../stores/cart'

const router = useRouter()
const authStore = useAuthStore()
const cartStore = useCartStore()

const user = computed(() => authStore.user)
const cartItemCount = computed(() => cartStore.totalItems)

onMounted(() => {
  cartStore.fetchCart()
})

const logout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.customer-layout {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.navbar {
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.nav-brand h2 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.nav-menu {
  display: flex;
  align-items: center;
  gap: 30px;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s;
  position: relative;
}

.nav-link:hover {
  color: #667eea;
  background-color: #f8f9ff;
}

.nav-link.router-link-active {
  color: #667eea;
  background-color: #f0f2ff;
}

.cart-link {
  position: relative;
}

.cart-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #ff4757;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-info span {
  color: #333;
  font-weight: 500;
}

.logout-btn {
  padding: 6px 12px;
  background: #ff4757;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background: #ff3742;
}

.main-content {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}
</style>